package org.mindrive.system.service.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.collection.system.DictionaryEntity;
import org.mindrive.system.collection.system.DictionaryTypeEntity;
import org.mindrive.system.core.MindriveException;
import org.mindrive.system.parameter.BaseSearchParameter;
import org.mindrive.system.parameter.system.DictionaryParameter;
import org.mindrive.system.parameter.system.SearchDictionaryParameter;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.util.InputStream2Str;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Service
@Slf4j
public class DictionaryService extends BaseService {


    public Map<String, Object> searchTypes(BaseSearchParameter parameter) {

        QueryWrapper<DictionaryTypeEntity> query = new QueryWrapper<DictionaryTypeEntity>();
        if (parameter.getName() != null && !"".equals(parameter.getName())) {
            query.like(NAME, parameter.getName());
        }

        int count = dictionaryTypeDao.count(query);

        List<DictionaryTypeEntity> dictionaryTypeList = new ArrayList<DictionaryTypeEntity>();
        Page<DictionaryTypeEntity> page = page(DictionaryTypeEntity.class, parameter.getPageSize(),
                parameter.getCurrentPage());
        if (page == null) {
            dictionaryTypeList = dictionaryTypeDao.list(query);
        } else {
            dictionaryTypeDao.page(page, query);
            dictionaryTypeList = page.getRecords();
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(COUNT, count);
        result.put(LIST, dictionaryTypeList);
        return result;
    }

    public void createDictionary(DictionaryParameter parameter) {

        DictionaryEntity dictionary = dictionaryDao.getOne(
                new QueryWrapper<DictionaryEntity>().eq(TYPE_ID, parameter.getTypeId()).eq(NAME, parameter.getName()),
                false);
        if (dictionary != null) {
            throw new MindriveException("名称已经存在");
        }
        dictionary = new DictionaryEntity();
        dictionary.setTypeId(parameter.getTypeId());
        dictionary.setName(parameter.getName());
        dictionary.setCondition(parameter.getCondition());
        dictionary.setCreatedAt(new Date());
        dictionaryDao.save(dictionary);

    }

    public void deleteDictionary(List<String> ids) {
        dictionaryDao.removeByIds(ids);
    }

    public void moidfyDictionary(String id, DictionaryParameter parameter) {

        DictionaryEntity dictionary = dictionaryDao.getOne(
                new QueryWrapper<DictionaryEntity>().eq(TYPE_ID, parameter.getTypeId()).eq(NAME, parameter.getName())
                        .ne(ID, id), false);
        if (dictionary != null) {
            throw new MindriveException("名称已经存在");
        }
        DictionaryEntity update = new DictionaryEntity();
        update.setId(id);
        update.setTypeId(parameter.getTypeId());
        update.setName(parameter.getName());
        update.setCondition(parameter.getCondition());
        dictionaryDao.updateById(update);

    }

    public Map<String, Object> searchDictionary(SearchDictionaryParameter parameter) {

        QueryWrapper<DictionaryEntity> query = new QueryWrapper<DictionaryEntity>();
        if (parameter.getTypeId() != null && !"".equals(parameter.getTypeId())) {
            query.eq(TYPE_ID, parameter.getTypeId());
        }
        if (parameter.getTypeCode() != null && !"".equals(parameter.getTypeCode())) {
            DictionaryTypeEntity type = dictionaryTypeDao.getOne(
                    new QueryWrapper<DictionaryTypeEntity>().eq(CODE, parameter.getTypeCode()), false);
            if (type == null) {
                query.eq(ID, KEY_NONE);
            } else {
                query.eq(TYPE_ID, type.getId());
            }
        }
        if (parameter.getName() != null && !"".equals(parameter.getName())) {
            query.like(NAME, parameter.getName());
        }
        if (parameter.getCondition() != null && !"".equals(parameter.getCondition())) {
            query.like(CONDITION(), parameter.getCondition());
        }

        int count = dictionaryDao.count(query);

        List<DictionaryEntity> dictionaryList = new ArrayList<DictionaryEntity>();
        Page<DictionaryEntity> page = page(DictionaryEntity.class, parameter.getPageSize(), parameter.getCurrentPage());
        if (page == null) {
            dictionaryList = dictionaryDao.list(query);
        } else {
            dictionaryDao.page(page, query);
            dictionaryList = page.getRecords();
        }

        Map<String, Object> result = new HashMap<String, Object>();
        result.put(COUNT, count);
        result.put(LIST, dictionaryList);
        return result;

    }

}
